package licode.unisop.single.user.infrastructure.repository.dao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import licode.unisop.provider.info.UserIdInfo;
import licode.unisop.single.user.application.entity.UserAuthEntity;
import licode.unisop.single.user.application.gateway.UserAuthDao;
import licode.unisop.single.user.infrastructure.assembler.UniUserSingleAssembler;
import licode.unisop.single.user.infrastructure.repository.mapper.UserAuthMapper;
import licode.unisop.single.user.infrastructure.repository.po.UserAuthPO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * uni-user-single
 *
 * @author licode
 * @date 2016-08-27 15:28:35
 */
@Component
public class UserAuthDaoImpl
        extends ServiceImpl<UserAuthMapper, UserAuthPO>
        implements UserAuthDao {
    @Resource
    private UniUserSingleAssembler uniUserSingleAssembler;

    @Override
    public void add(UserAuthEntity item) {
        super.save(uniUserSingleAssembler.fromUserAuthEntity(item));
    }

    @Override
    public void update(UserAuthEntity item) {
        super.updateById(uniUserSingleAssembler.fromUserAuthEntity(item));
    }

    @Override
    public void removeWithId(Serializable id) {
        super.removeById(id);
    }

    @Override
    public void remove(UserIdInfo idFind) {
        super.lambdaUpdate()
                .eq(UserAuthPO::getRealmId, idFind.getRealmId())
                .eq(UserAuthPO::getUserId, idFind.getUserId())
                .remove();
    }

    @Override
    public UserAuthEntity getWithId(Serializable id) {
        return uniUserSingleAssembler.fromUserAuth(super.getById(id));
    }

    @Override
    public UserAuthEntity findByUserId(UserIdInfo idFind) {
        return uniUserSingleAssembler.fromUserAuth(super.lambdaQuery()
                .eq(UserAuthPO::getRealmId, idFind.getRealmId())
                .eq(UserAuthPO::getUserId, idFind.getUserId())
                .one());
    }
}
